//
//  ZGSendAddressController.m
//  NewBee
//
//  Created by leergou on 16/8/21.
//  Copyright © 2016年 WhiteHouse. All rights reserved.
//

#import "ZGSendAddressController.h"
#import "ZGSendAddressCell.h"
#import "ZGSendAddressModel.h"
#import "ZGDetailAddressController.h"

static NSString *cellID = @"cellID";

@interface ZGSendAddressController ()<UITableViewDelegate,UITableViewDataSource>


// 背景图片
@property (nonatomic,strong) UIImageView *backgroundImageView;
// 提示label
@property (nonatomic,strong) UILabel *noticeLabel;
// 添加地址按钮
@property (nonatomic,strong) UIButton *addButton;
// footerView
@property (nonatomic,strong) UIView *bottomView;


// tableView
@property (nonatomic,strong) UITableView *tableView;
// 地址数组
@property (nonatomic,strong) NSMutableArray<ZGSendAddressModel *> *addressList;

@end

@implementation ZGSendAddressController
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];
    
    [self setupNav];
    
    
}



#pragma mark - 按钮点击 -
- (void)addAddr:(UIButton *)sender{
    
     ZGDetailAddressController *detailAddressVc = [[ZGDetailAddressController alloc] init];
    
    // 设置操作类型
    detailAddressVc.operationType = OperationTypeAdd;
    
    __weak typeof (detailAddressVc) weakdetailAddressVc = detailAddressVc;
     __weak typeof(self) weakSelf = self;
    
    detailAddressVc.callBack = ^(OperationType operationType){
        
        if (operationType == OperationTypeAdd){
            
            [weakSelf.addressList addObject:weakdetailAddressVc.addressModel];
            
            NSLog(@"%ld",weakSelf.addressList.count);
        }
        
         [weakSelf.tableView reloadData];
        
    };
    
    [self.navigationController pushViewController:detailAddressVc animated:YES];
}

- (void)backButtonClick:(UIButton *)sender{
    [self.navigationController popViewControllerAnimated:YES];
    
}


#pragma mark - tableView data source & delegate -
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.addressList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    ZGSendAddressCell  *cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath];
    
    cell.detailAddressModel = self.addressList[indexPath.row];
    
    __weak typeof(self) weakSelf = self;
    
    cell.buttonClick = ^{
        // 点击 编辑 按钮 开始编辑
        [weakSelf startEdit:indexPath];
    };
    
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    UIView *footView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 44)];
    
    return footView;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    // 点击 cell 开始编辑
    [self startEdit:indexPath];
}

#pragma mark - 设置UI -
- (void)setupUI{
    
    self.view.backgroundColor = RGBCOLOR(239, 239, 239);
    // 底部工具条
    [self.view addSubview:self.bottomView];
    [self.bottomView addSubview:self.addButton];
    
    [self.bottomView makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(49);
        make.left.bottom.right.equalTo(self.view);
    }];
    
    [self.addButton makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.bottomView);
    }];
    
    // 中间视图
    if (self.addressList.count == 0) {
        // 没有收获地址,加载背景图片
        [self showBackgroudView];
        
    } else {
        // 有收货地址
        [self showAdressListView];
    }
}

#pragma mark - 设置导航 -
- (void)setupNav{
    
    self.navigationItem.title = @"我的收货地址";
    [self.navigationController.navigationBar setBarTintColor:RGBCOLOR(249, 250, 253)];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"v2_goback"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(backButtonClick:)];
    
}

#pragma mark - 自定义方法 -

- (void)startEdit:(NSIndexPath *)indexPath{
    
    NSLog(@"编辑");
    __weak typeof(self) weakSelf = self;
    
    ZGDetailAddressController *detailAddressVc = [[ZGDetailAddressController alloc] init];
    
    // 设置操作类型
    detailAddressVc.operationType = OperationTypeEdite;
    
    // 给 detailAddressVc一个 block,这样回调,删除当前地址
    detailAddressVc.callBack = ^(OperationType operationType){
        
        if (operationType == OperationTypeDelete) {
            
            [weakSelf.addressList removeObject:weakSelf.addressList[indexPath.row]];
            
        }
        
        [weakSelf.tableView reloadData];
    };
    
    [self.navigationController pushViewController:detailAddressVc animated:YES];
    
    detailAddressVc.addressModel = weakSelf.addressList[indexPath.row];
}

- (void)showAdressListView{
    // 添加子控件
    [self.view addSubview:self.tableView];
    // 添加约束
    [self.tableView makeConstraints:^(MASConstraintMaker *make) {
        make.top.leading.trailing.equalTo(self.view);
        make.bottom.equalTo(self.bottomView.top);
    }];
    
    
}
- (void)showBackgroudView{
    // 添加子控件
    [self.view addSubview:self.backgroundImageView];
    [self.view addSubview:self.noticeLabel];
    // 添加约束
    [self.backgroundImageView makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.centerY.equalTo(self.view).offset(-50);
    }];
    
    [self.noticeLabel makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.top.equalTo(self.backgroundImageView.bottom).offset(10);
    }];
}


#pragma mark - lazy -
- (UIImageView *)backgroundImageView{
    
    if (!_backgroundImageView) {
        _backgroundImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"v2_address_empty"]];
    }
    return _backgroundImageView;
}

- (UILabel *)noticeLabel{
    if (!_noticeLabel) {
        _noticeLabel = [[UILabel alloc] init];
        _noticeLabel.text = @"您还没有地址哦~";
        _noticeLabel.font = [UIFont systemFontOfSize:12];
        _noticeLabel.textColor = [UIColor lightGrayColor];
        _noticeLabel.textAlignment = NSTextAlignmentCenter;
        [_noticeLabel sizeToFit];
    }
    return _noticeLabel;
}

- (UIButton *)addButton{
    if (!_addButton) {
        _addButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_addButton setTitle:@"+ 新增地址" forState:UIControlStateNormal];
        [_addButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        _addButton.titleLabel.font = [UIFont systemFontOfSize:12];
        [_addButton setBackgroundImage:[UIImage imageNamed:@"icon_sendcoupon"] forState:UIControlStateNormal];
        [_addButton setBackgroundImage:[UIImage imageNamed:@"icon_sendcoupon"] forState:UIControlStateHighlighted];
        [_addButton addTarget:self action:@selector(addAddr:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _addButton;
}

- (UIView *)bottomView{
    if (!_bottomView) {
        _bottomView = [[UIView alloc] initWithFrame:CGRectZero];
        _bottomView.backgroundColor = [UIColor whiteColor];
    }
    return _bottomView;
}

- (NSMutableArray<ZGSendAddressModel *> *)addressList{
    if (!_addressList) {
        _addressList = [NSMutableArray array];
        
        ZGSendAddressModel *model1 = [ZGSendAddressModel new];
        model1.name = @"小王";
        model1.telNum = @"1234455";
        model1.city = @"兰州市";
        model1.zone = @"新开区";
        model1.detailAddr = @"七里河";
        model1.gender = @"先生";
        
        ZGSendAddressModel *model2 = [ZGSendAddressModel new];
        model2.name = @"小的";
        model2.telNum = @"4857662";
        model2.city = @"荷兰市";
        model2.zone = @"看到区";
        model2.detailAddr = @"轮力度";
        model2.gender = @"女士";
        
        ZGSendAddressModel *model3 = [ZGSendAddressModel new];
        model3.name = @"小了";
        model3.telNum = @"784997263";
        model3.city = @"宁波市";
        model3.zone = @"奉献区";
        model3.detailAddr = @"临床宽度";
        model3.gender = @"先生";

        [_addressList addObject:model1];
        [_addressList addObject:model2];
        [_addressList addObject:model3];
        
    }
    return _addressList;
}

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero];
        _tableView.rowHeight = 50;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.tableFooterView = [[UIView alloc] init];
        _tableView.backgroundColor = kGlobalGray;
        
        [_tableView registerClass:[ZGSendAddressCell class] forCellReuseIdentifier:cellID];
    }
    return _tableView;
}


@end
